﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;

public class SocketConnect : MonoBehaviour
{
	//public Socket m_Socket;
	//private void Awake()
	//{
	//	Socket m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
	//}
	bool m_Active = false;
	Socket m_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
	SocketMsgCreater m_MsgCreater = new SocketMsgCreater(2048);

	HCService.Client.ClientStream client;

	public void Connect()
	{
		IPAddress ip = IPAddress.Parse("127.0.0.1");
		IPEndPoint port = new IPEndPoint(ip, 11);

		m_client.Connect(port);
		m_Active = true;
		//StartCoroutine(CorSend());
		client = new HCService.Client.ClientStream(m_client);
	}


	protected void ConnectCallBack(IAsyncResult iar)
	{
		//m_Active = true;
		//StartCoroutine(CorSend());
	}
	IEnumerator CorSend()
	{
		int count = 0;
		while (m_Active)
		{

			try
			{
				//byte[] outPack = null;
				m_MsgCreater.Clear();
				string tag = "Debuf";
				byte[] headTag = Encoding.UTF8.GetBytes(tag);

				
				byte[] content = Encoding.UTF8.GetBytes($"Send {count++} Msg");

				var totalLength = headTag.Length + content.Length +  2 * sizeof(int);
				m_MsgCreater.Write(totalLength);
				m_MsgCreater.Write(headTag.Length);
				m_MsgCreater.Write(headTag);
				m_MsgCreater.Write(content);

				//outPack = new byte[totalLength + sizeof(int)];
				//byte[] packSize = BitConverter.GetBytes(totalLength);
				//byte[] headSize = BitConverter.GetBytes(headTag.Length);

				//Array.Copy(packSize, outPack, packSize.Length);
				//Array.Copy(headSize, 0, outPack, packSize.Length, headSize.Length);
				//Array.Copy(headTag, 0, outPack, packSize.Length + headSize.Length, headTag.Length);
				//Array.Copy(content, 0, outPack, packSize.Length + headSize.Length + headTag.Length, content.Length);
				m_client.Send(m_MsgCreater.Buffer,0, m_MsgCreater.Position, SocketFlags.None);
				Debug.Log("发送消息");
			}
			catch (Exception e)
			{
				Debug.Log($"发送失败 {e.StackTrace}");
				break;
			}
			
			yield return new WaitForSeconds(2f);
		}
	}

	IEnumerator CorSendInt()
	{
		int count = 0;
		while (m_Active)
		{
			using (MemoryStream ms = new MemoryStream())
			{
				using (BinaryWriter bw = new BinaryWriter(ms))
				{

					count++;
					byte[] content = BitConverter.GetBytes(count);
					for (int i = 0; i < content.Length; i++)
					{
						Debug.Log($"第{count}个数字的 {i} 字节");
						m_client.Send(new byte[1] { content[i] });
						yield return new WaitForSeconds(2f);
					}
				}
			}
			yield return new WaitForSeconds(2f);
		}
	}

	private void Update()
	{
		client?.DoUpdate();
		if (Input.GetKeyUp(KeyCode.F2)) {

			try
			{
				m_MsgCreater.Clear();
				string tag = "Request_Res";
				byte[] headTag = Encoding.UTF8.GetBytes(tag);
				byte[] content = Encoding.UTF8.GetBytes("TempName");
				var totalLength = headTag.Length + content.Length + 2 * sizeof(int);

				m_MsgCreater.Write(totalLength);
				m_MsgCreater.Write(headTag.Length);
				m_MsgCreater.Write(headTag);
				m_MsgCreater.Write(content);

				m_client.Send(m_MsgCreater.Buffer, 0, m_MsgCreater.Position, SocketFlags.None);
			}
			catch
			{
				Debug.Log("发送失败");
			}
		}
	}


	void SendProto()
	{
		
	}
}
